import pygame

from PyPixelCore.Setting import *
from PyPixelCore.block.Block import Block
from PyPixelCore.block.TextButton import TextButton

"""
    SimplifyButton --> TextButton --> TextBlock --> Block --> BaseBlock
    简化的文字按钮
    可直接使用或者继承优化
    功能:
        显示文字
        支持碰撞检测
        支持帧函数与计时器函数
        支持点击事件
        支持鼠标移入移出事件
        不支持自动换行绘制
        简化按钮,更加简洁的按钮
"""


class SimplifyButton(TextButton):
    def __init__(self, text_content="未定义按钮", point_x=0, point_y=0, layer=0):
        self.text_surf_shadow = None
        super().__init__(text_content, point_x, point_y, 0, 0, 0, layer, True)
        self.font_color = Color_type.BLACK
        self.hover_color = Color_type.GREY
        self.reload_text_surf()
        self.is_dirty = True

    def mouse_hover_enter(self):
        self.now_font_color = self.hover_color
        self.reload_text_surf()
        self.is_dirty = True

    def mouse_hover_leave(self):
        self.now_font_color = self.font_color
        self.reload_text_surf()
        self.is_clicked = False
        self.is_dirty = True

    def click_down(self, pos):  # 可以优化,虽然并不怎么消耗内存
        self.click_down_func(self)
        self.is_clicked = True
        self.now_font_color = self.press_color
        self.reload_text_surf()
        self.is_dirty = True

    def click_up(self):
        self.click_up_func(self)
        self.is_clicked = False
        self.is_dirty = True

    def set_color_no_None(self, font_color=None, bk_color=None, border_color=None, hover_color=None, press_color=None):
        if font_color is not None:
            self.font_color = font_color
        if press_color is not None:
            self.press_color = press_color
        self.is_dirty = True

    def _reload_image_first(self):
        self.now_font_color = self.font_color
        self.text_surf = self.font.render(self.text_content, True, self.now_font_color)
        self.text_surf_shadow = self.font.render(self.text_content, True, self.font_color)
        t_width = self.text_surf.get_size()[0] \
            if self.width == 0 else self.width
        t_height = self.text_surf.get_size()[1] \
            if self.height == 0 else self.height
        self.image = pygame.Surface((t_width, t_height), pygame.SRCALPHA)
        self.image.fill((0, 0, 0, 0))
        self.rect.size = self.image.get_size()

        # 绘制文字到主图上
        self.image.blit(self.text_surf,
                        ((self.rect.w - self.text_surf.get_size()[0]) // 2,
                         (self.rect.h - self.text_surf.get_size()[1]) // 2,
                         self.rect.width, self.rect.height))
        self.collision_rect.size = self.rect.size

    def reload_image(self):
        if not self.is_dirty:
            return
        self.is_dirty = False
        self.image.fill((0, 0, 0, 0))

        # 绘制文字到主图上
        self.image.blit(self.text_surf,
                        ((self.rect.w - self.text_surf.get_size()[0]) // 2,
                         (self.rect.h - self.text_surf.get_size()[1]) // 2,
                         self.rect.width, self.rect.height))
        if self.is_clicked:
            self.image.blit(self.text_surf_shadow,
                            ((self.rect.w - self.text_surf.get_size()[0]) // 2 + 1,
                             (self.rect.h - self.text_surf.get_size()[1]) // 2 + 1,
                             self.rect.width, self.rect.height))

    def reload_text_surf(self):
        self.text_surf = self.font.render(self.text_content, True, self.now_font_color)
        self.text_surf_shadow = self.font.render(self.text_content, True, self.font_color)
        t_width = self.text_surf.get_size()[0] \
            if self.width == 0 else self.width
        t_height = self.text_surf.get_size()[1] \
            if self.height == 0 else self.height
        self.image = pygame.Surface((t_width, t_height), pygame.SRCALPHA)
        self.image.fill((0, 0, 0, 0))
        self.rect.size = self.image.get_size()
        self.collision_rect.size = self.rect.size

    def draw(self, surf):
        surf.blit(self.image, self.rect)
        if show_rect_collision:
            pygame.draw.rect(surf, Color_type.RED, self.rect, width=block_surface_width)


def basic_mouse_down(self: SimplifyButton):
    pass


def basic_mouse_up(self: SimplifyButton):
    pass
